package com.icinfo.cloud.provider.punish.config.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.icinfo.cloud.common.core.page.PageResponse;
import com.icinfo.cloud.common.security.util.UserUtil;
import com.icinfo.cloud.provider.common.exception.BusinessException;
import com.icinfo.cloud.provider.punish.config.mapper.ConfigPoliceAdjudicationMapper;
import com.icinfo.cloud.provider.punish.config.model.ConfigPoliceAdjudication;
import com.icinfo.cloud.provider.punish.config.service.IConfigPoliceAdjudicationService;
import com.icinfo.cloud.provider.punish.ucase.casemain.dto.PoliceAdjudicationDto;
import com.icinfo.cloud.provider.punish.ucase.casemain.dto.PoliceAdjudicationModifyDto;
import com.icinfo.cloud.provider.punish.ucase.casemain.vo.PoliceAdjudicationVo;
import com.icinfo.cloud.xzcf.provider.member.userinfo.dto.UserDTO;
import com.icinfo.framework.common.web.Result;
import com.icinfo.framework.core.service.support.MyBatisServiceSupport;
import com.icinfo.framework.mybatis.mapper.entity.Example;
import com.icinfo.framework.mybatis.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

import static cn.hutool.core.date.DateTime.now;

@Service
@Slf4j
public class ConfigPoliceAdjudicationServiceImpl extends MyBatisServiceSupport implements IConfigPoliceAdjudicationService {
    @Resource
    private ConfigPoliceAdjudicationMapper configPoliceAdjudicationMapper;

    /**
     * 描述： 公安裁量和记分配置查询
     *
     * @return
     * @author: lujiayu
     * @date: 2024/1/2
     **/
    @Override
    public PageResponse<PoliceAdjudicationVo> getPoliceAdjudicationPageList(PoliceAdjudicationDto policeAdjudicationDto) {
        PageHelper.startPage(policeAdjudicationDto.getPageNum(), policeAdjudicationDto.getLength());
        List<ConfigPoliceAdjudication> configPoliceAdjudicationList = configPoliceAdjudicationMapper.pageList(policeAdjudicationDto);
        PageResponse<ConfigPoliceAdjudication> pageResponse = new PageResponse<>(configPoliceAdjudicationList);
        return pageResponse.convert(e -> {
            PoliceAdjudicationVo policeAdjudicationVo = new PoliceAdjudicationVo();
            BeanUtils.copyProperties(e, policeAdjudicationVo);
            return policeAdjudicationVo;
        });
    }

    @Override
    public Result<String> updatePoliceAdjudication(PoliceAdjudicationModifyDto policeAdjudicationModifyDto) {
        UserDTO userInfo = UserUtil.getUserInfo();
        if (ObjectUtil.isEmpty(userInfo) || StringUtils.isBlank(userInfo.getUserId().toString())) {
            throw new BusinessException("登录用户信息获取失败");
        }
        // 检查记分依据与扣分标准的填写情况
        policeAdjudicationModifyDto.checkScoringBasis();
        ConfigPoliceAdjudication query = new ConfigPoliceAdjudication();
        query.setItemCode(policeAdjudicationModifyDto.getItemCode());
        ConfigPoliceAdjudication policeAdjudication = configPoliceAdjudicationMapper.selectOne(query);
        if (ObjectUtil.isEmpty(policeAdjudication)) {
            throw new BusinessException("未查询到裁量和记分相关配置");
        }
        // 组装修改后的数据并跟新
        policeAdjudication.setUpdateTime(now());
        policeAdjudication.setUpdateUserId(userInfo.getPhone());
        policeAdjudication.setDiscretionaryBasis(policeAdjudicationModifyDto.getDiscretionaryBasis());
        policeAdjudication.setScoringBasis(policeAdjudicationModifyDto.getScoringBasis());
        policeAdjudication.setScoringArticleNumber(policeAdjudicationModifyDto.getScoringArticleNumber());
        policeAdjudication.setScoringStyleNumber(policeAdjudicationModifyDto.getScoringStyleNumber());
        policeAdjudication.setScoringItemNumber(policeAdjudicationModifyDto.getScoringItemNumber());
        policeAdjudication.setLicenseScore(policeAdjudicationModifyDto.getLicenseScore());
        configPoliceAdjudicationMapper.updateByPrimaryKey(policeAdjudication);
        return Result.success("修改成功");
    }

    @Override
    public PoliceAdjudicationVo getByItemRelationCode(String itemRelationCode) {
        if (StringUtils.isBlank(itemRelationCode)) {
            return null;
        }
        Example example = new Example(ConfigPoliceAdjudication.class);
        example.createCriteria().andEqualTo("itemRelationCode", itemRelationCode);
        ConfigPoliceAdjudication configPoliceAdjudication = new ConfigPoliceAdjudication();
        configPoliceAdjudication.setItemRelationCode(itemRelationCode);
        ConfigPoliceAdjudication policeAdjudication = configPoliceAdjudicationMapper.selectOne(configPoliceAdjudication);
        if (ObjectUtil.isEmpty(policeAdjudication)) {
            return null;
        }
        PoliceAdjudicationVo policeAdjudicationVo = new PoliceAdjudicationVo();
        BeanUtils.copyProperties(policeAdjudication, policeAdjudicationVo);
        return policeAdjudicationVo;
    }
}
